package com.aiyige.page.my.message;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventNewMessageChanged;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.track.annotation.Track;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Track("我的消息")
@Route(path = ARouterConfig.MessageCenterPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class MessageCenterPage extends BaseActivity {
    public static final int MESSAGE_LEVEL = 1;
    @BindView(R.id.inputEt)
    EditText inputEt;
    @BindView(R.id.notifyMessageUnReadNumTv)
    TextView notifyMessageUnReadNumTv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.notifyMessageContentTv)
    TextView notifyMessageContentTv;
    @BindView(R.id.inputEtClearBtn)
    ImageView inputEtClearBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_message_center);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.my_message);
        EventBus.getDefault().register(this);
        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                inputEtClearBtn.setVisibility(s.length() == 0 ? View.INVISIBLE : View.VISIBLE);
            }
        });
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateView();
    }

    public void updateView() {
        MessageUtil.MessageCount messageCount = MessageUtil.getNewMessageCount(MESSAGE_LEVEL);
        long totalMessageNum = MessageUtil.getTotalNewMessageCount(messageCount);
        String message = "";
        if (totalMessageNum > 0) {
            notifyMessageUnReadNumTv.setVisibility(View.VISIBLE);
            notifyMessageUnReadNumTv.setText(MessageUtil.formatUnReadMessageNum(totalMessageNum));
        } else {
            notifyMessageUnReadNumTv.setVisibility(View.INVISIBLE);
        }
        notifyMessageContentTv.setText(message);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventReceiveNewMessage(EventNewMessageChanged event) {
        updateView();
    }

    @OnClick({R.id.titleBackBtn, R.id.sysNotifyLayout, R.id.inputEtClearBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inputEtClearBtn:
                inputEt.setText("");
                break;
            case R.id.titleBackBtn:
                finish();
                break;

            case R.id.sysNotifyLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.NotifyMessageEntryPage)
                        .navigation();
                MessageUtil.clearNewMessage(MESSAGE_LEVEL);
                break;
        }
    }
}
